home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Magazine / C_Tutorial / Part-9 / wb2 / loadsave.c < prev    next >
C/C++ Source or Header  |  1998-02-01  |  4KB  |  152 lines

  1. #include "loadsave.h"
  2. #include "bitmap.h"
  3. #include "drawwin.h"
  4. #include "gui.h"
  5.  
  6. #include "iff.h"
  7.  
  8. #include<libraries/asl.h>
  9. #include<intuition/intuition.h>
  10.  
  11. #include<string.h>
  12. #include<stdio.h>
  13.  
  14. #include<clib/asl_protos.h>
  15. #include<clib/dos_protos.h>
  16. #include<clib/graphics_protos.h>
  17.  
  18. /* The size of our filename string */
  19. #define MAXFILENAME        (300)
  20.  
  21. /* Global handles for our requesters */
  22. static struct FileRequester* loadreq = NULL;
  23. static struct FileRequester* savereq = NULL;
  24.  
  25. /* Open an ASL load file requester */
  26. int load()
  27. {
  28.     /* Allocate the requester if we haven't already */
  29.     if(loadreq == NULL)
  30.         loadreq = (struct FileRequester*)
  31.             AllocAslRequestTags(ASL_FileRequest,
  32.                                                     ASLFR_TitleText,            "Load File",
  33.                                                     ASLFR_Flags1,                    FRF_DOPATTERNS,
  34.                                                     ASLFR_InitialPattern,    "#?.iff",
  35.                                                     TAG_DONE);
  36.     if(loadreq)
  37.     {
  38.         struct Window* win = getDrawWin();
  39.         if(AslRequestTags(loadreq, ASLFR_Window, win, TAG_DONE))
  40.         {
  41.             char filename[MAXFILENAME];
  42.             /* Create complete filename from ASL's dir and file */
  43.             strcpy(filename, loadreq->rf_Dir);
  44.             if(AddPart(filename, loadreq->rf_File, MAXFILENAME))
  45.                 return loadfile(filename);
  46.             else
  47.                 printf("Error: could not make filename\n");
  48.         }
  49.         /* else: requester was cancelled */
  50.     }
  51.     else
  52.         printf("Error: could not allocate ASL (load) file request\n");
  53.     /* If we get this far then there was no fatal error */
  54.     return TRUE;
  55. }
  56.  
  57. int loadfile(char* filename)
  58. {
  59.     /* Record any fatal errors */
  60.     int going = TRUE;
  61.     IFFL_HANDLE handle;
  62.     /* Try to open the IFF file */
  63.     if(handle = IFFL_OpenIFF(filename, IFFL_MODE_READ))
  64.     {
  65.         UWORD colortable[256];
  66.         /* Get colour information */
  67.         LONG count = IFFL_GetColorTab(handle, colortable);
  68.         /* Get display information */
  69.         ULONG displayid = IFFL_GetViewModes(handle);
  70.         /* Get picture information */
  71.         struct IFFL_BMHD* bmhd = IFFL_GetBMHD(handle);
  72.         struct Window* win;
  73.         /* Try to adjust the screen to fit */
  74.         if(bmhd)
  75.         {
  76.             closeGUI();
  77.             /* If this fails, our local win will be NULL */
  78.             openGUI(bmhd->nPlanes, bmhd->w, bmhd->h, displayid);
  79.         }
  80.         if(win = getDrawWin())
  81.         {
  82.             /* Change screen colours */
  83.             LoadRGB4(&(win->WScreen->ViewPort), colortable, count);
  84.             /* If we can load the picture, update window's display */
  85.             if(IFFL_DecodePic(handle, getBitmap()))
  86.                 CopySBitMap(win->WLayer);
  87.             else
  88.                 printf("Error: could not decode IFF picture\n");
  89.         }
  90.         else
  91.             going = FALSE;  /* The only fatal error */
  92.         IFFL_CloseIFF(handle);
  93.     }
  94.     else
  95.         printf("Error: could not open IFF file\n");
  96.     return going;
  97. }
  98.  
  99. /* Open an ASL save file requester */
  100. void save()
  101. {
  102.     /* Another way of saying "allocate if we haven't already" */
  103.     if(savereq ||
  104.         (savereq = (struct FileRequester*)
  105.             AllocAslRequestTags(ASL_FileRequest,
  106.                                                     ASLFR_TitleText,            "Save File",
  107.                                                     ASLFR_Flags1,                    FRF_DOPATTERNS | FRF_DOSAVEMODE,
  108.                                                     ASLFR_InitialPattern,    "#?.iff",
  109.                                                     ASLFR_InitialFile,        "picture.iff",
  110.                                                     TAG_DONE)))
  111.     {
  112.         struct Window* win = getDrawWin();
  113.         if(AslRequestTags(savereq, ASLFR_Window, win, TAG_DONE))
  114.         {
  115.             char filename[MAXFILENAME];
  116.             /* Create complete filename from ASL's dir and file */
  117.             strcpy(filename, savereq->rf_Dir);
  118.             if(AddPart(filename, savereq->rf_File, MAXFILENAME))
  119.             {
  120.                 /* Make sure our bitmap is the same as the display */
  121.                 SyncSBitMap(win->WLayer);
  122.                 /* Try saving our bitmap, using the screen's colours */
  123.                 if(IFFL_SaveBitMap(filename, getBitmap(),
  124.                                                     win->WScreen->ViewPort.ColorMap->ColorTable,
  125.                                                     IFFL_COMPR_BYTERUN1) == 0)
  126.                     printf("Error: could not write IFF picture\n");
  127.             }
  128.             else
  129.                 printf("Error: could not make filename\n");
  130.         }
  131.         /* else: requester was cancelled */
  132.     }
  133.     else
  134.         printf("Error: could not allocate ASL (save) file request\n");
  135. }
  136.  
  137. /* Free any requesters that may have been allocated */
  138. void freeReqs()
  139. {
  140.     if(loadreq)
  141.     {
  142.         FreeAslRequest(loadreq);
  143.         loadreq = NULL;
  144.     }
  145.     if(savereq)
  146.     {
  147.         FreeAslRequest(savereq);
  148.         savereq = NULL;
  149.     }
  150. }
  151.  
  152.